package com.echo.util;

import com.activeandroid.Model;
import com.echo.model.User;
import com.echo.serializer.UserSerializer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Modifier;
import java.util.Date;
import java.util.Calendar;

/**
 * Created by EchoGe on 2016/6/2.
 */
public class DateUtil {

    public static void main(String[] args){
        String json="{\"id\":20,\"gender\":0,\"username\":\"qwert\",\"headImageUrl\":\"http:\\/\\/123.206.205.201\\/sports\\/iis-85.png\",\"password\":\"123456\",\"nickname\":\"yyy\",\"age\":0}";
        Gson gson = new GsonBuilder().setExclusionStrategies(new ActiveAndroidStrategy(null, Model.class)).serializeNulls().create();
        User  user= (User) new UserSerializer().deserialize(json);
        user.getHeadImage();
    }

    /**
     * 设置时间
     * @param year
     * @param month
     * @param date
     * @return
     */
    public static Calendar setCalendar(int year,int month,int date){
        Calendar cl = Calendar.getInstance();
        cl.set(year, month-1, date);
        return cl;
    }

    /**
     * 获取当前时间的前一天时间
     * @param cl
     * @return
     */
    public static Calendar getBeforeDay(Calendar cl){
        //使用roll方法进行向前回滚
        //cl.roll(Calendar.DATE, -1);
        //使用set方法直接进行设置
        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day-1);
        return cl;
    }

    /**
     * 获取当前时间的后一天时间
     * @param cl
     * @return
     */
    public static Calendar getAfterDay(Calendar cl){
        //使用roll方法进行回滚到后一天的时间
        //cl.roll(Calendar.DATE, 1);
        //使用set方法直接设置时间值
        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day+1);
        return cl;
    }

    /**
     * 打印时间
     * @param cl
     */
    public static void printCalendar(Calendar cl){
        int year = cl.get(Calendar.YEAR);
        int month = cl.get(Calendar.MONTH)+1;
        int day = cl.get(Calendar.DATE);
        System.out.println(year+"-"+month+"-"+day);
    }

    /**
     * 获取当前一周的日期字符串
     */
    public static String[] getThisWeekDate(){
        String[] dates=new String[7];
        Calendar cl=Calendar.getInstance();
        int todayInThisWeek=cl.getTime().getDay();
        if(todayInThisWeek==0)
            todayInThisWeek=7;
        for(int i=todayInThisWeek;i>0;i--){
            dates[i-1]=String.valueOf(cl.getTime().getDate());
            cl=getBeforeDay(cl);
        }
        cl=Calendar.getInstance();
        for(int i=todayInThisWeek+1;i<8;i++){
            cl=getAfterDay(cl);
            dates[i-1]=String.valueOf(cl.getTime().getDate());
        }
        return dates;
    }

    /**
     * 获取当前一周的Calendar
     * @return
     */
    public static Calendar[] getThisWeekCalendar(){
        Calendar[] dates=new Calendar[7];
        Calendar cl=Calendar.getInstance();
        int todayInThisWeek=cl.getTime().getDay();
        if(todayInThisWeek==0)
            todayInThisWeek=7;
        for(int i=todayInThisWeek;i>0;i--){
            dates[i-1]= (Calendar) cl.clone();
            cl=getBeforeDay(cl);
        }
        cl=Calendar.getInstance();
        for(int i=todayInThisWeek+1;i<8;i++){
            cl=getAfterDay(cl);
            dates[i-1]= (Calendar) cl.clone();
        }
        return dates;
    }

    /**
     * 获取之前7天（包括今天）的Calendar
     * @return
     */
    public static Calendar[] getPreWeekCalendar(){
        Calendar[] dates=new Calendar[7];
        Calendar cl=Calendar.getInstance();
        for(int i=0;i<7;i++){
            dates[6-i]= (Calendar) cl.clone();
            cl=getBeforeDay(cl);
        }
        return dates;
    }

    /**
     * 获取之前30天（包括今天）的Calendar
     * @return
     */
    public static Calendar[] getPreMonthCalendar(){
        Calendar[] dates=new Calendar[30];
        Calendar cl=Calendar.getInstance();
        for(int i=0;i<30;i++){
            dates[29-i]= (Calendar) cl.clone();
            cl=getBeforeDay(cl);
        }
        return dates;
    }

    /**
     * 获取Calendar类型转换成的年月日字符串
     * @param cl
     * @return
     */
    public static String getDateStr(Calendar cl){
        String str="";
        Date time= cl.getTime();
        int year=time.getYear();
        int month=time.getMonth()+1;
        int date=time.getDate();
        str+=year;
        if(month<10)
            str+="0";
        str+=month;
        if(date<10)
            str+="0";
        str+=date;
        return str;
    }

    /**
     * 获取年月日字符串，格式为yyyy/mm/dd
     */
    public static String getDateStr1(String date){
        String year=date.substring(0,3);
        String month=date.substring(3,5);
        String day=date.substring(5,7);
        return "2016/"+month+"/"+day;
    }
}
